package cn.zhentao.config;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

public class MyBatisConfig {

    //配置SqlSessionFactory
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean session = new SqlSessionFactoryBean();
        //别名
        session.setTypeAliasesPackage("cn.zhentao.pojo");
        //数据源
        session.setDataSource(dataSource);
        //mapper映射
        session.setMapperLocations(new ClassPathResource("mapper/CardMapper.xml")
        ,new ClassPathResource("mapper/PersonMapper.xml"));
        //开启驼峰命名
        Configuration config = new Configuration();
        config.setMapUnderscoreToCamelCase(true);
        session.setConfiguration(config);

        return session;
    }

    //配置映射接口
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("cn.zhentao.dao");
        return msc;
    }
}
